package component.controls.ui
{
	import component.controls.ScrollBar;
	import component.controls.TextArea;
	import component.global.ComponentSetting;
	import component.global.utils.DisplayUtils;
	import component.global.utils.GraphicsUtils;
	import component.global.utils.PositionUtils;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class TAScrollBar extends ScrollBar
	{
		public function TAScrollBar($height:int=ComponentSetting.SCROLL_BAR_DEFAULT_HEIGHT)
		{
			super($height);
		}
		
		override protected function init():void{
			super.init();
			_scrollUnit = 1;
		}
		
		override public function set targetDisplay(displayObject:DisplayObject):void{
			if(displayObject is TextArea){
				_targetDisplay = displayObject
				if(_viewPort == null){
					_viewPort = GraphicsUtils.createRectangle(_targetDisplay.width, height);
					_viewPort = new Shape();
					_viewPort.graphics.beginFill(0x000000,0.1);
					_viewPort.graphics.drawRect(0,0,_targetDisplay.width+2, height+2);
					_viewPort.graphics.endFill();
				}
				PositionUtils.setPos(_viewPort,_targetDisplay.localToGlobal(new Point(_targetDisplay.x,_targetDisplay.y)));
				_targetDisplay.mask = _viewPort;
				DisplayUtils.addDisplayObject(_targetDisplay as TextArea,_viewPort);
			} else {
				super.targetDisplay = displayObject;
			}
		}
		
		override protected function __onThumbMove(e:MouseEvent):void{
			var unitPixs:int = (height - _decreaseBtn.height - _increaseBtn.height)/ (_targetDisplay as TextArea).textField.numLines;
			if(Math.abs(_thumbBtn.mouseY - _mouseY) >= unitPixs){
				scrollTo(_scrollPos + (_thumbBtn.mouseY > _mouseY ? 1 : -1));
					_mouseY = _thumbBtn.mouseY;
			}
		}
		
		override public function updateThumb():void{
			if(_targetDisplay && _viewPort){
				var ta:TextArea = _targetDisplay as TextArea;
				DisplayUtils.addDisplayObject(this,_thumbBtn);
				_thumbBtn.height = (height - _decreaseBtn.height - _increaseBtn.height) * (ta.textField.numLines - ta.textField.maxScrollV + 1) / ta.textField.numLines;
				_scrollPos = ta.textField.scrollV - 1;
				_thumbBtn.y = (height - _decreaseBtn.height - _increaseBtn.height) * _scrollPos / ta.textField.numLines + _decreaseBtn.height;
			} else {
				DisplayUtils.removeDisplay(_thumbBtn);
			}
		}
		
		override protected function __onBtnDownHandler(e:MouseEvent):void{
			e.stopImmediatePropagation();
			switch(e.target){
				case _decreaseBtn:
					_timerScrollUnit = -_scrollUnit;
					scrollTo(_scrollPos - _scrollUnit);
					break;
				case _increaseBtn:
					_timerScrollUnit = _scrollUnit;
					scrollTo(_scrollPos + _scrollUnit);
					break;
				case this:
					if(mouseY > _thumbBtn.y){
						_timerScrollUnit = _scrollUnit;
						scrollTo(_scrollPos + _scrollUnit);
					} else {
						_timerScrollUnit = - _scrollUnit;
						scrollTo(_scrollPos - _scrollUnit);
					}
					break;
				default:
					return;
			}
			_btnTimer.start();
		}
		
		override public function scrollTo(pos:int):void{
			updateThumb();
			if(_scrollPos == pos)return;
			var ta:TextArea = _targetDisplay as TextArea;
			var tempPos:int = pos;
			var minPos:int = 0;
			var maxPos:int = ta.textField.maxScrollV - 1;
			if(tempPos > maxPos){
				_scrollPos = maxPos;
			} else if(tempPos < minPos){
				_scrollPos = minPos;
			} else {
				_scrollPos = tempPos;
			}
			ta.textField.scrollV = _scrollPos + 1;
			_thumbBtn.y = (height - _decreaseBtn.height - _increaseBtn.height) * _scrollPos / ta.textField.numLines + _decreaseBtn.height;
		}
	}
}